//
//  ConsulationAnswerCell.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/2.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "ConsulationAnswerCell.h"

@interface ConsulationAnswerCell ()

/// viewModle
@property (nonatomic, readwrite, strong) ConsulationAnswerCellModel *viewModel;

/// IBOutlet
@property (weak, nonatomic) IBOutlet UILabel* nameLabel;
@property (weak, nonatomic) IBOutlet UILabel* contentLabel;
@property (weak, nonatomic) IBOutlet UILabel* timeLabel;
@property (weak, nonatomic) IBOutlet UIImageView* avatarImgView;
@property (weak, nonatomic) IBOutlet UIButton* focusBtn;
@property (weak, nonatomic) IBOutlet UIButton* adoptBtn;

@end

@implementation ConsulationAnswerCell

- (void)awakeFromNib
{
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

#pragma mark - bind data
- (void)bindViewModel:(ConsulationAnswerCellModel *)viewModel
{
    self.viewModel = viewModel;
    
    @weakify(self);
    
    self.nameLabel.text = self.viewModel.name;
    self.contentLabel.text = self.viewModel.content;
    self.timeLabel.text = self.viewModel.time;
    [self.avatarImgView yy_setImageWithURL:[NSURL URLWithString:self.viewModel.imgurl] placeholder:MHImageNamed(@"placeholderLogo")];
    
    [[RACObserve(self.viewModel, isAdopt) takeUntil:[self rac_prepareForReuseSignal]] subscribeNext:^(id x) {
        
        @strongify(self);
        BOOL boolValue = [x boolValue];
        
        self.adoptBtn.enabled = !boolValue;
    }];
    
    [[RACObserve(self.viewModel, isAnswered) takeUntil:[self rac_prepareForReuseSignal]] subscribeNext:^(id x) {
        
        @strongify(self);
        
        BOOL isAnswered = [x boolValue];
        
        BOOL isShow = YES;
        
        if (isAnswered)
        {
            if (!self.viewModel.isAdopt)
                isShow = NO;
        }
        else
        {
            isShow = self.viewModel.isAskUser;
        }
        
        self.adoptBtn.hidden = !isShow;
    }];
    
    [[[self.focusBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:[self rac_prepareForReuseSignal]] subscribeNext:^(id x)
    {
        @strongify(self);
        
        [self.viewModel.focusCommand execute:nil];
    }];
    
    [[[self.adoptBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:[self rac_prepareForReuseSignal]] subscribeNext:^(id x)
     {
         @strongify(self);
         
         [self.viewModel.adoptCommand execute:nil];
     }];
    
    [[[self rac_signalForSelector:@selector(layoutSubviews)] takeUntil:[self rac_prepareForReuseSignal]] subscribeNext:^(id x)
     {
         @strongify(self);
         
         self.avatarImgView.layer.cornerRadius = self.avatarImgView.frame.size.width/2.0f;
         self.avatarImgView.layer.masksToBounds = YES;
     }];
}

@end
